package trading.ctm.yunjing.module

import android.net.http.SslError
import android.os.Bundle
import android.view.View
import android.webkit.SslErrorHandler
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import cc.vv.baselibrary.activity.BaseActivity
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.baselibrary.view.share.BCShareDialog
import cc.vv.baselibrary.view.share.BCShareInter
import kotlinx.android.synthetic.main.activity_web.*
import trading.ctm.yunjing.R

class WebActivity : BaseActivity() {
    override fun initLayoutRes(): Int {
        return R.layout.activity_web
    }

    override fun initView(bundle: Bundle?) {
        super.initView(bundle)
        btbv_web_title.setTitle("paimaixiaoxi")
    }

    override fun initData(data: Bundle?) {
        super.initData(data)
        web_view_content.loadUrl("https://mp.weixin.qq.com/s/sEavV4f7UBbG3mpluJzPDA")
        web_view_content.webViewClient = MyWebViewClient()
        web_view_content.webChromeClient = MyWebCromeClient()
    }

    override fun initAction(data: Bundle?) {
        super.initAction(data)

        btbv_web_title.setEventInterface(object : BaseTopBarView.EventInterface {
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {
            }

            override fun rightOnClick() {
                BCShareDialog.getInstance().initDialog(this@WebActivity, object : BCShareInter {
                    override fun onShareSuccess(s: String?) {

                    }

                    override fun onShareCancel() {

                    }

                    override fun onShareError(e: String?) {

                    }
                })


            }

        })

    }

    /**
     * MyWebViewClient
     */
    private inner class MyWebViewClient : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            view.loadUrl(url)
            return true
        }

        override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {}

        override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
            //接受所有证书
            handler.proceed()
        }

        override fun onPageFinished(view: WebView, url: String) {
            super.onPageFinished(view, url)
            runOnUiThread {
                //                setTitleContent()
            }
        }
    }


    private inner class MyWebCromeClient : WebChromeClient() {

        override fun onProgressChanged(view: WebView, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            if (newProgress == 100) {
                //加载完毕进度条消失
                pv_jobWeb_progressBar.visibility = View.GONE
            } else {
                //更新进度
                pv_jobWeb_progressBar.setProgress(newProgress)
            }

        }

    }

}